<?php
/*
Copyright (C) 2009 de Flotte Maxence

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
?>
<?php
//Column
class std_COLUMN extends Bout {
  private $isWritable = false;

  function setIsWritable($Bool) {
    if($Bool == '1' OR $Bool == 'True')
      $this->isWritable = true;
    else
      $this->isWritable = false;
  }

  private function createTable($_InfoColumn,$_Column) {
    //On creer de la liste
    $result = '<table><tr>';
    foreach($_InfoColumn as $_Info) {
      $result .= '<th>'.$_Info->name.'</th>';
    }
    $result .= '</tr>';
    if($_Column != null)
      foreach($_Column as $nLigne => $ligne) {
	$result .= '<tr>';
	foreach($ligne as $champs) {
	  $result .= '<td>'.$champs.'</td>';
	}
	$result .= '</tr>';
      }
    if($this->isWritable) {
      $result .= '<tr><form method="GET" action="">';
      foreach($_InfoColumn as $_Info) {
	$result .= '<td><input type="text" name="'.$_Info->name.'"/></td>';
      }
      $result .= '<input type="submit" style="visibility: hidden;" /></form></tr>';
    }
    $result .= '</table>';
    return $result;
  }

  private function readFlux() {
    if(isset($this->_Flux))
      foreach($this->_Flux as $flux) {
	if($flux['type'] == 'sql') {
	  //connection à MySql
	  $MySql = $GLOBALS['MySql'];
	  //On créer la requete
	  $query = $MySql->query($flux['flux']);
	  //On renseigne les titres des colones
	  $fieldInfo = $query->fetch_fields();
	  foreach ($fieldInfo as $val) {
	    $_InfoColumn[] = $val;
	    //$table = $val->table;
	  }
	  //On regarde si il y a demande d'enregistrement
	  if(isset($_GET[$_InfoColumn[0]->name]) and $this->isWritable) {
	    foreach($_GET as $key => $value) {
	      $requetePart1 .= $key.',';
	      $requetePart2 .= "'".$value."',";
	    }
	    $requetePart1 = '('.preg_replace('!.$!','',$requetePart1).')';
	    $requetePart2 = '('.preg_replace('!.$!','',$requetePart2).')';
	    $requete = 'INSERT INTO '.$_InfoColumn[0]->table.$requetePart1.' VALUES'.$requetePart2;
	    $MySql->query($requete);
	    $query = $MySql->query($flux['flux']);
	  }

	  //On créer le tableau contenant les informations
	  $i = 0;
	  while ($ligne = $query->fetch_assoc()) {
	    foreach($ligne AS $key=>$info) {
	      $_Column[$i][] = $info;
	    }
	    $i++;
	  }

	  //Si il n'y a pas d'entrÃ©
	  /*if($this->titleColumn == null)
	    if(preg_match('!^SELECT\s?\*\s?FROM .*$!',$Flux)) {
	      $query = preg_replace('!^SELECT\s?\*\s?FROM\s?(\w*).?$!','$1',$Flux);
	      $query = $MySql->query('SHOW COLUMNS FROM '.$query);
	      while ($ligne = $query->fetch_assoc())
		$this->titleColumn[] = $ligne['Field'];
	      //query("SHOW COLUMNS FROM sometable");
	    }*/
	}
	$return .= $this->createTable($_InfoColumn,$_Column);
      }
    return $return;
  }

  private function showTables() {
    if(isset($this->_Flux))
      $return = $this->readFlux();
    return $return;
  }

  function build() {
    return $this->showTables();
  }
}
?>